export function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode | null {
  if (!root1) return root2;
  if (!root2) return root1;
  const fn = (node1: TreeNode | null, node2: TreeNode | null): TreeNode | null => {
    if (!node1) return node2;
    if (!node2) return node1;
    node1.val = node1.val + node2.val;
    node1.left = fn(node1.left, node2.left);
    node1.right = fn(node1.right, node2.right);
    return node1;
  };

  return fn(root1, root2);
}
